package cc.rengu.igas.channel.upqc.core.service.trans;

import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.QueryService;
import cc.rengu.igas.channel.upqc.facade.request.QrCodeQueryRequest;
import cc.rengu.igas.channel.upqc.facade.response.QrCodeQueryResponse;
import cc.rengu.igas.share.facade.request.UpqcQrCodeQueryRequest;
import cc.rengu.igas.share.facade.response.UpqcQrCodeQueryResponse;
import cc.rengu.igas.share.facade.result.Result;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RandomUtil;

/**
 * 银联二维码订单查询服务
 * * 查询订单 UP->AC
 * * reqType:0120000903
 * * 有银联向收单侧发起二维码交易查询
 */
public class QrCodeQueryService extends QueryService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        QrCodeQueryRequest qrCodeQueryRequest = new QrCodeQueryRequest();
        ConvertUtil.convertOutput(qrCodeQueryRequest);
        return qrCodeQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        QrCodeQueryRequest qrCodeQueryRequest = (QrCodeQueryRequest) request;
        QrCodeQueryResponse qrCodeQueryResponse = new QrCodeQueryResponse();

        /* 调用Share模块中的银联二维码业务公共处理服务 */
        UpqcQrCodeQueryRequest upqcQrCodeQueryRequest = new UpqcQrCodeQueryRequest();
        BeanUtil.beanCopy(qrCodeQueryRequest, upqcQrCodeQueryRequest);
        upqcQrCodeQueryRequest.getHeader().setTxnNum("P01SH002");
        upqcQrCodeQueryRequest.getHeader().setTraceNo(ChannelEnum.UPQC.getChannelType() + DateUtil.getCurrentDateTime("yyyyMMddHHmmssSSS") + RandomUtil.randomNumStr(10));
        DubboService dubboService = new DubboServiceImpl();
        Result<UpqcQrCodeQueryResponse> callResult = (Result<UpqcQrCodeQueryResponse>) dubboService.callDubboService("upqcUnifiedService", "qrCodeQuery", upqcQrCodeQueryRequest);
        if (null != callResult) {
            UpqcQrCodeQueryResponse upqcQrCodeQueryResponse = callResult.getResult();
            BeanUtil.beanCopy(upqcQrCodeQueryResponse, qrCodeQueryResponse);
        }

        /* 组装应答报文 */
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(qrCodeQueryResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        QrCodeQueryResponse qrCodeQueryResponse = (QrCodeQueryResponse) response.getResult();
        ConvertUtil.convertInput(qrCodeQueryResponse);
    }
}
